package cn.edu.xmu.orderpayment.order.controller;

import cn.edu.xmu.oomall.core.util.Common;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.orderpayment.order.model.vo.NewAfterSaleOrderVo;
import cn.edu.xmu.orderpayment.order.service.ShopOrderService;
import cn.edu.xmu.privilegegateway.annotation.aop.Audit;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginName;
import cn.edu.xmu.privilegegateway.annotation.aop.LoginUser;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import static cn.edu.xmu.oomall.core.util.Common.processFieldErrors;

/**
 * @author Chencheng Liao 22920192204238
 * @date 2021/12/22
 */
@Api(value = "内部的订单api")
@Validated
@RefreshScope
@RestController
@RequestMapping(value = "/", produces = "application/json;charset=UTF-8")
public class InternalOrderController {
    @Autowired
    private HttpServletResponse httpServletResponse;

    @Autowired
    private ShopOrderService shopOrderService;

    /**
     * 内部API-确认团购订单
     *
     * @param
     * @return Object
     * @author Chencheng Liao 22920192204238
     * @Date 2021/12/12
     */
    @ApiOperation(value = "确认团购订单")
    @PutMapping("internal/shops/{shopId}/grouponorders/{id}/confirm")
    @Audit
    public InternalReturnObject confirmGroupOnOrder(@PathVariable Long shopId, @PathVariable Long id, @LoginUser Long userId, @LoginName String userName) {

        ReturnObject res = shopOrderService.confirmGroupOnOrder(userId, userName, shopId, id);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }


    /**
     * 内部API-取消订单
     *
     * @param
     * @return Object
     * @author Chencheng Liao 22920192204238
     * @Date 2021/12/28
     */
    @Audit
    @ApiOperation(value = "内部API-取消订单")
    @PutMapping("internal/shops/{shopId}/orders/{id}/cancel")
    public InternalReturnObject cancelOrder(@PathVariable Long shopId, @PathVariable Long id, @LoginUser Long userId, @LoginName String userName) {

        ReturnObject res = shopOrderService.cancelOrder(shopId, id, userId, userName);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }

    @Audit(departName = "shops")
    @ApiOperation(value = "内部API-管理员建立售后订单")
    @PostMapping("internal/shops/{shopId}/orders")
    public InternalReturnObject creatAfterSaleOrder(@PathVariable Long shopId, @Validated @RequestBody NewAfterSaleOrderVo newAfterSaleOrderVo, @LoginUser Long loginUserId, @LoginName String loginName, BindingResult bindingResult) {

        if(shopId!=0)
            return new InternalReturnObject(ReturnNo.RESOURCE_ID_OUTSCOPE.getCode(),ReturnNo.RESOURCE_ID_OUTSCOPE.getMessage());

        Object returnObject = processFieldErrors(bindingResult, httpServletResponse);
        if (null != returnObject) {
            return new InternalReturnObject(ReturnNo.FIELD_NOTVALID.getCode(),ReturnNo.FIELD_NOTVALID.getMessage());
        }

        ReturnObject res = shopOrderService.createAfterSaleOrder(newAfterSaleOrderVo, loginUserId, loginName);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        if(res.getCode()!=ReturnNo.OK){
            return ret;
        }
        httpServletResponse.setStatus(HttpServletResponse.SC_CREATED);
        return ret;
    }

    /**
     * 内部API，根据id获得orderItem
     * @param id
     * @return
     */
    @GetMapping("/internal/orderitems/{id}")
    public InternalReturnObject getOrderItemInfoById(@PathVariable("id") Long id){
        ReturnObject res=shopOrderService.getOrderItemById(id);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }

    /**
     * 用orderSn查询Order
     * @param orderSn
     * @return
     */
    @GetMapping("/internal/orders")
    public InternalReturnObject getOrderBySn(@RequestParam("orderSn") String orderSn){
        ReturnObject res = shopOrderService.getOrderDetailByOrderSn(orderSn);

        InternalReturnObject ret = new InternalReturnObject();
        if(res.getData()!=null)
            ret.setData(res.getData());
        ret.setErrno(res.getCode().getCode());
        ret.setErrmsg(res.getErrmsg());
        return ret;
    }
}
